/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.frames;

import ore.Frame;
import ore.Transform;
import ore.errors.OrekitException;
import ore.time.AbsoluteDate;

import org.apache.commons.math.geometry.Rotation;
import org.apache.commons.math.geometry.Vector3D;

/** 
 * Mean Equator, Mean Equinox Frame.  It is sometimes called Mean of
 * Date (MoD) frame.
 * 
 * This frame handles precession effects according to the IAU-76 model (Lieske).
 * 
 * Its parent frame is the GCRF frame.
 * 
 * @author Pascal Parraud
 */
public class MEMEFrame
    extends Frame
{
    /** 
     * 1st coefficient for ZETA precession angle.
     */
    private static final double ZETA_1 = 2306.2181   * RADIANS_PER_ARC_SECOND;
    /** 
     * 2nd coefficient for ZETA precession angle.
     */
    private static final double ZETA_2 =    0.30188  * RADIANS_PER_ARC_SECOND;
    /** 
     * 3rd coefficient for ZETA precession angle.
     */
    private static final double ZETA_3 =    0.017998 * RADIANS_PER_ARC_SECOND;
    /**
     * 1st coefficient for THETA precession angle.
     */
    private static final double THETA_1 = 2004.3109   * RADIANS_PER_ARC_SECOND;
    /** 
     * 2nd coefficient for THETA precession angle.
     */
    private static final double THETA_2 =   -0.42665  * RADIANS_PER_ARC_SECOND;
    /** 
     * 3rd coefficient for THETA precession angle.
     */
    private static final double THETA_3 =   -0.041833 * RADIANS_PER_ARC_SECOND;
    /** 
     * 1st coefficient for Z precession angle.
     */
    private static final double Z_1 = 2306.2181   * RADIANS_PER_ARC_SECOND;
    /** 
     * 2nd coefficient for Z precession angle.
     */
    private static final double Z_2 =    1.09468  * RADIANS_PER_ARC_SECOND;
    /**
     * 3rd coefficient for Z precession angle.
     */
    private static final double Z_3 =    0.018203 * RADIANS_PER_ARC_SECOND;



    private AbsoluteDate cachedDate;
    /** 
     * EOP history (null if EOP are ignored).
     */
    private final EOP1980History eopHistory;


    /** 
     * Apply EOP corrections (EME2000/GCRF bias compensation).
     * 
     * @param date the date.
     * @param name name of the frame
     * 
     * @exception OrekitException EOP parameters cannot be read
     */
    protected MEMEFrame(AbsoluteDate date, String name)
        throws OrekitException
    {
        this(true, date, name);
    }
    /** 
     * @param applyEOPCorr Apply EOP corrections (EME2000/GCRF bias
     * compensation)
     * @param date Current date.
     * @param name Name of the frame
     * 
     * @exception OrekitException EOP parameters cannot be read
     */
    protected MEMEFrame(boolean applyEOPCorr, AbsoluteDate date, String name)
        throws OrekitException
    {
        super(applyEOPCorr ? FramesFactory.getGCRF() : FramesFactory.getEME2000(), null , name);

        this.eopHistory = applyEOPCorr ? new EOP1980History() : null;
        /*
         * Everything is in place, we can now synchronize the frame
         */
        this.updateFrame(date);
    }


    /** 
     * The update includes the precession effects.
     * 
     * @param date new value of the date
     */
    protected void updateFrame(AbsoluteDate date) {
        if ((this.cachedDate == null) || !this.cachedDate.equals(date)) {
            Rotation precession;
            {
                /*
                 * Offset from J2000 epoch in julian centuries
                 */
                final double tts = date.durationFrom(AbsoluteDate.J2000_EPOCH);
                final double ttc = tts * JULIAN_CENTURY_PER_SECOND;
                /*
                 * Compute the zeta precession angle
                 */
                final double zeta = ((ZETA_3 * ttc + ZETA_2) * ttc + ZETA_1) * ttc;
                /*
                 * Compute the theta precession angle
                 */
                final double theta = ((THETA_3 * ttc + THETA_2) * ttc + THETA_1) * ttc;
                /*
                 * Compute the z precession angle
                 */
                final double z = ((Z_3 * ttc + Z_2) * ttc + Z_1) * ttc;
                /*
                 * Elementary rotations for precession
                 */
                Rotation r1 = new Rotation(Vector3D.PLUS_K,  z);
                Rotation r2 = new Rotation(Vector3D.PLUS_J, -theta);
                Rotation r3 = new Rotation(Vector3D.PLUS_K,  zeta);
                /*
                 * Complete precession
                 */
                precession = r1.applyTo(r2.applyTo(r3));
            }
            /*
             * Set up the transform from parent GCRF
             */
            this.setTransform(new Transform(precession));

            this.cachedDate = date;
        }

    }
    /**
     * The data provided comes from the IERS files. It is smoothed
     * data.
     * 
     * @param date date at which the value is desired
     * 
     * @return Value of UT1-UTC, in seconds (0 if date is outside covered range)
     */
    double getUT1MinusUTC(AbsoluteDate date) {
        return (eopHistory == null) ? 0.0 : eopHistory.getUT1MinusUTC(date);
    }
    /** 
     * The data provided comes from the IERS files. It is smoothed
     * data.
     * 
     * @param date date at which the value is desired
     * 
     * @return LoD (Length of Day) value in seconds (0 if date is
     * outside range
     */
    double getLOD(AbsoluteDate date) {
        return (eopHistory == null) ? 0.0 : eopHistory.getLOD(date);
    }
    /** 
     * Get the correction to the nutation parameters.
     * 
     * The data provided comes from the IERS files. It is smoothed data.
     * 
     * @param date date at which the correction is desired
     * 
     * @return Nutation correction, or the null correction for date
     * outside range.
     */
    NutationCorrection getNutationCorrection(AbsoluteDate date) {
        return (eopHistory == null) ?
               NutationCorrection.NULL_CORRECTION : eopHistory.getNutationCorrection(date);
    }
}
